package com.zis.wechat.api.utils;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.util.EntityUtils;

/**
 * Created by wangshen on 2017/8/23.
 */
public class HttpRequest {

	public static DefaultHttpClient m_httpclient;

    static {
        m_httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager());
    }

    /**
     * httpclient post
     * @param url 请求URL
     * @param param 请求param
     * @return 返回为空字符表示程序出现错误
     */
    public static String doPost(String url, String param) {
        HttpPost httpPost = new HttpPost(url);
        try {
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
            httpPost.setEntity(new StringEntity(param, "utf-8"));
            HttpResponse response = m_httpclient.execute(httpPost);
            String result = EntityUtils.toString(response.getEntity(),"utf-8");
            return result;
        } catch (Exception e) {
            return "";
        }finally {
        }
    }
}
